import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

 /// author：lx
 /// date：2021-10-12
 /// descr：新版本提示界面，点击跳转到外部浏览器
class UpdatePointWidget extends StatefulWidget {
  final String? checkVersionUrl;
  const UpdatePointWidget({Key? key,this.checkVersionUrl}): super(key: key);
  @override
  _UpdatePointWidgetState createState() => _UpdatePointWidgetState();
}

class _UpdatePointWidgetState extends State<UpdatePointWidget> {


  @override
  Widget build(BuildContext context) {
    return (!(widget.checkVersionUrl??'').isNotEmpty)?Container(width: 0,):
    GestureDetector(
      onTap: ()=>_toUpdate(widget.checkVersionUrl),
      child: Container(
        width: MediaQuery.of(context).size.width,
        decoration: BoxDecoration(
            color: Colors.white,
          borderRadius: BorderRadius.circular(5)
        ),
        padding:const EdgeInsets.only(
          left: 12,
          right: 12,
          top: 8,
          bottom: 8,
        ),
        child: Row(
          children:const [
            Icon(
              Icons.error_outline,
              size: 24,
              color: Colors.red,
            ),
            SizedBox(width: 15,),
            Expanded(child: Text(
              '您有新的版本，去更新',
              style: TextStyle(
                  color: Color(0xffd81e06),
                fontWeight: FontWeight.w600
              ),
            )),
            SizedBox(width: 15,),
            Icon(
              Icons.arrow_forward_ios_rounded,
              size: 7,
            )

          ],
        ),
      ),
    );
  }
  //跳转到外部浏览器打开
  _toUpdate(String? checkVersionUrl) async{
    checkVersionUrl=checkVersionUrl??'';
    if (await canLaunch(checkVersionUrl)) {
      launch(checkVersionUrl);
    } else {
      SnackBar snackBar =
      const SnackBar(content:
       Text('打开浏览器失败'));
      ScaffoldMessenger.of(context).showSnackBar(snackBar);
    }
  }
}
